< previous page page_163 next page >

Page 163
End Type

Private Sub Command1_Click()
        Dim MyPoint As Point
        MyPoint.x = 3
        MyPoint.y = 5
End Sub
As you can see from the sample, you declare a new type by using the Type statement. Note that types must be defined in the general declarations section; in this case, we have defined a private type called Point within a form. To create a variable of the new type, you use the Dim statement just as you would with any other type. The parts of a type are accessed via dot notation.
As you know, there are some specialized types to deal with databases (such as Database, Field, and Recordset). Visual Basic knows about these other data types when you add a reference to a type library. A type library is a DLL or other file that makes these types available to your program. A good example of using a type library is discussed in Chapter 22, "Using OLE to Control Other Applications."
sarrow.gifSee "The Microsoft Word Object Library" p. 489
Variable Declarations
Earlier, in the section "Naming Variables," you saw an example of the Dim statement, which is used to tell Visual Basic the name and type of your variable. However, Visual Basic does not require you to specifically declare a variable before it is used. If a variable is not declared, Visual Basic creates the variable by using a default data type usually a variant. A variant can contain any type of information. Using a variant for general information has two major drawbacksit can waste memory resources, and the variable type might be invalid for use with functions that expect a specific variable type.
Always declaring your variables before they are used is good programming practice. Therefore, you should now take a look at the two ways to declare a variable in Visual Basicexplicit and implicit declarationsand the special case of fixed-length strings.
Explicit Declaration
Explicit declaration means that you use statements to define the names and types of your program's variables. These statements do not assign values to the variables but merely tell Visual Basic what the variables should be called and what type of data they can contain.
You can use each of the following statements to explicitly declare a variable's type:
Dim varname [As vartype][, varname2 [As vartype2]]

Private varname [As vartype][, varname2 [As vartype2]]

Static varname [As vartype][, varname2 [As vartype2]]

Public varname [As vartype][, varname2 [As vartype2]]

 
< previous page page_163 next page >